import request from '@/utils/request'

// 查询保养单列表
export function listMaintenanceRecord(query) {
  return request({
    url: '/ldzl-sb/sb/maintenance/record/list',
    method: 'get',
    params: query
  })
}

// 查询保养单详细
export function getMaintenanceRecord(id) {
  return request({
    url: '/ldzl-sb/sb/maintenance/record/' + id,
    method: 'get'
  })
}

// 开始执行保养
export function startMaintenance(recordId) {
  return request({
    url: '/ldzl-sb/sb/maintenance/record/start-maintenance/' + recordId,
    method: 'post'
  })
}

// 新增保养单
export function addMaintenanceRecord(data) {
  // 预处理日期时间格式
  const processedData = { ...data };
  
  // 如果存在maintenanceStartTime且是字符串形式
  if (processedData.maintenanceStartTime && typeof processedData.maintenanceStartTime === 'string') {
    // 尝试提取有效的日期时间格式
    try {
      const dateStr = processedData.maintenanceDate; // 应该是 yyyy-MM-dd 格式
      
      // 提取时间部分
      const timeParts = processedData.maintenanceStartTime.split(' ');
      let timeStr = '';
      
      // 从多种可能的格式中提取时间
      for (const part of timeParts) {
        if (part.includes(':') && !part.includes('GMT')) {
          timeStr = part;
          break;
        }
      }
      
      // 如果找到有效时间格式
      if (timeStr) {
        // 确保时间格式为 HH:mm:ss
        if (timeStr.split(':').length === 2) {
          timeStr += ':00'; // 添加秒
        }
        
        // 组合成标准格式
        processedData.maintenanceStartTime = `${dateStr} ${timeStr}`;
        console.log('处理后的maintenanceStartTime:', processedData.maintenanceStartTime);
      } else {
        // 无法提取时间，删除该字段
        delete processedData.maintenanceStartTime;
        console.warn('无法提取有效时间格式，已删除maintenanceStartTime字段');
      }
    } catch (e) {
      console.error('日期时间格式处理错误:', e);
      delete processedData.maintenanceStartTime;
    }
  }
  
  return request({
    url: '/ldzl-sb/sb/maintenance/record',
    method: 'post',
    data: processedData
  })
}

// 修改保养单
export function updateMaintenanceRecord(data) {
  return request({
    url: '/ldzl-sb/sb/maintenance/record',
    method: 'put',
    data: data
  })
}

// 删除保养单
export function delMaintenanceRecord(id) {
  return request({
    url: '/ldzl-sb/sb/maintenance/record/' + id,
    method: 'delete'
  })
}

// 查询保养单列表（包含关联信息）
export function listMaintenanceRecordWithInfo(query) {
  return request({
    url: '/ldzl-sb/sb/maintenance/record/listWithInfo',
    method: 'get',
    params: query
  })
}

// 根据保养计划ID获取保养单列表
export function getRecordsByPlanId(planId) {
  return request({
    url: '/ldzl-sb/sb/maintenance/record/plan/' + planId,
    method: 'get'
  })
}

// 根据设备ID获取保养单列表
export function getRecordsByEquipmentId(equipmentId) {
  return request({
    url: '/ldzl-sb/sb/maintenance/record/equipment/' + equipmentId,
    method: 'get'
  })
}

// 生成保养单编号
export function generateRecordCode() {
  return request({
    url: '/ldzl-sb/sb/maintenance/record/code/generate',
    method: 'get'
  })
}

// 获取保养计划列表
export function getMaintenancePlanList() {
  return request({
    url: '/ldzl-sb/sb/maintenance/plan/list',
    method: 'get'
  })
}

// 获取设备列表
export function getEquipmentList() {
  return request({
    url: '/ldzl-sb/sb/equipment/list',
    method: 'get'
  })
} 